package com.theo.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.theo.entity.FactInfo;
import com.theo.entity.TopicInfo;
import com.theo.entity.TypeInfo;
import com.theo.service.FactInfoService;
import com.theo.service.TypeInfoService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 方涛
 * @since 2021-02-02
 */
@Controller
@RequestMapping("/fact-info")
public class FactInfoController {

    @Autowired
    private FactInfoService factInfoService;
    @Autowired
    private TypeInfoService typeInfoService;

    @RequestMapping("/show")
    public String showList(Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 8);
        List<FactInfo> factInfos = factInfoService.queryAllFactInfo();
        PageInfo pageInfo = new PageInfo(factInfos, 2);
        model.addAttribute("factInfos", factInfos);
        List<TypeInfo> typeInfos = typeInfoService.queryFactTypeInfo();
        model.addAttribute("typeInfos", typeInfos);
        return "user/fact_list";
    }
    @RequestMapping("/show.do")
    public String show(Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 8);
        List<FactInfo> list = factInfoService.queryAllFactInfo();
        PageInfo pageInfo = new PageInfo(list, 2);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/fact_list";
    }
    @RequestMapping("/toInput.do")
    public String toInput(Model model) {
        List<TypeInfo> typeInfos = typeInfoService.queryFactTypeInfo();
        model.addAttribute("typeInfos", typeInfos);
        return "admin/fact_input";
    }

    @RequestMapping("/input.do")
    public String input(FactInfo factInfo) {
        factInfoService.saveFactInfo(factInfo);
        return "redirect:/fact-info/show.do";
    }

    @RequestMapping("/delete.do")
    public String delete(Integer id) {
        factInfoService.deleteFactInfo(id);
        return "redirect:/fact-info/show.do";
    }

    @RequestMapping("/toUpdate.do")
    public String toUpdate(Model model, Integer id) {
        FactInfo factInfo = factInfoService.queryFactInfoById(id);
        model.addAttribute("fact", factInfo);
        List<TypeInfo> typeInfos = typeInfoService.queryFactTypeInfo();
        model.addAttribute("typeInfos", typeInfos);
        return "admin/fact_update";
    }

    @RequestMapping("/update.do")
    public String update(FactInfo factInfo) {
        factInfoService.updateFactInfo(factInfo);
        return "redirect:/fact-info/show.do";
    }
}

